package com.paas.demo.service;

import com.github.pagehelper.PageHelper;
import com.paas.demo.common.PPageInfo;
import com.paas.demo.mybatis.mapper.UserInfoMapper;
import com.paas.demo.mybatis.model.UserInfo;
import com.paas.demo.mybatis.model.UserInfoExample;
import io.shardingsphere.api.HintManager;
import io.shardingsphere.core.hint.HintManagerHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class DemoService {

    @Autowired
    UserInfoMapper userInfoMapper;

    public static int userId = 150;

    public void demo() {
        System.out.println("Insert--------------");

        for (int i = 1; i <= 10; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setAccount("Account" + i);
            userInfo.setPassword("pass" + i);
            userInfo.setUserName("name" + i);
            userId++;
            if(i==3){
                HintManagerHolder.clear();
                HintManager hintManager = HintManager.getInstance();
                //hintManager.addDatabaseShardingValue("user_info", "user_id", 3L);
                //hintManager.addTableShardingValue("user_info", "user_id", 3L);
                System.out.println(userId);
            }
            userInfoMapper.insert(userInfo);
//

//


        }
        System.out.println("over..........");
    }
    public PPageInfo<UserInfo> queryForListPage(Map<String, Object> params, int page, int size){
    	PageHelper.startPage(page, size);
    	
    	UserInfoExample example = new UserInfoExample();
    	example.setOrderByClause("user_id desc");
    	List<UserInfo> users = userInfoMapper.selectByExample(example);
    	
		PPageInfo<UserInfo> pageInfo = new PPageInfo<UserInfo>(users);
		
		return pageInfo;

    }
}
